package com.jdy.haoduoaiteacher.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.jdy.haoduoaiteacher.Constant;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.ShowMessageFromWX;
import com.tencent.mm.sdk.modelmsg.WXAppExtendObject;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

  // private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;
  private IWXAPI api;
  @SuppressWarnings("unused")
  private String channel = "weixin_session";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.activity_managebuy);

    api = WXAPIFactory.createWXAPI(this, Constant.WX_APP_ID, false);
    api.handleIntent(getIntent(), this);
  }

  @Override
  protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    setIntent(intent);
    api.handleIntent(intent, this);
  }

  // @Override
  // public void onReq(BaseReq req) {
  // // try {
  // // SendMessageToWX.Req realReq = (Req) req;
  // // channel = realReq.scene == SendMessageToWX.Req.WXSceneSession ?
  // // "weixin_session"
  // // : "weixin_timeline";
  // // } catch (Exception e) {
  // // // TODO: handle exception
  // // }
  // //
  // // // Log.e("WinXin", "Showing");
  // // switch (req.getType()) {
  // // case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
  // // goToGetMsg();
  // // break;
  // // case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
  // // goToShowMsg((ShowMessageFromWX.Req) req);
  // // break;
  // // default:
  // // break;
  // // }
  // }

  // @Override
  // public void onResp(BaseResp resp) {
  // switch (resp.errCode) {
  // case BaseResp.ErrCode.ERR_OK:
  // Toast.makeText(this, "分享成功", Toast.LENGTH_LONG).show();
  // break;
  // case BaseResp.ErrCode.ERR_USER_CANCEL:
  // Toast.makeText(this, "用户取消", Toast.LENGTH_LONG).show();
  // break;
  // case BaseResp.ErrCode.ERR_AUTH_DENIED:
  // Toast.makeText(this, "认证失败", Toast.LENGTH_LONG).show();
  // break;
  // default:
  // Toast.makeText(this, "未知错误" + resp.errCode, Toast.LENGTH_LONG).show();
  //
  // break;
  // }
  // this.finish();
  // }

  @SuppressWarnings("unused")
  private void goToGetMsg() {
    // Intent intent = new Intent(this, HomeActivity.class);
    // this.startActivity(intent);
  }

  @SuppressWarnings("unused")
  private void goToShowMsg(ShowMessageFromWX.Req showReq) {
    WXMediaMessage wxMsg = showReq.message;
    WXAppExtendObject obj = (WXAppExtendObject) wxMsg.mediaObject;

    StringBuffer msg = new StringBuffer();
    msg.append("description: ");
    msg.append(wxMsg.description);
    msg.append("\n");
    msg.append("extInfo: ");
    msg.append(obj.extInfo);
    msg.append("\n");
    msg.append("filePath: ");
    msg.append(obj.filePath);
    Toast.makeText(this, msg.toString(), Toast.LENGTH_LONG).show();
  }

  @Override
  public void onReq(BaseReq arg0) {
    // TODO Auto-generated method stub

  }

  @Override
  public void onResp(BaseResp resp) {
    switch (resp.errCode) {
    case BaseResp.ErrCode.ERR_OK:
      Toast.makeText(this, "分享成功", Toast.LENGTH_LONG).show();
      break;
    case BaseResp.ErrCode.ERR_USER_CANCEL:
      Toast.makeText(this, "用户取消", Toast.LENGTH_LONG).show();
      break;
    case BaseResp.ErrCode.ERR_AUTH_DENIED:
      Toast.makeText(this, "认证失败", Toast.LENGTH_LONG).show();
      break;
    default:
      Toast.makeText(this, "未知错误" + resp.errCode, Toast.LENGTH_LONG).show();

      break;
    }
    this.finish();

  }

}